package ua.org.extjsbuilder.parser.runtime;

import ua.org.extjsbuilder.parser.IToken;
import ua.org.extjsbuilder.parser.ITokenScanner;
import ua.org.extjsbuilder.parser.JSParseException;

public abstract class AbstractToken implements IToken {
	protected AbstractToken next;
	protected AbstractToken previous;
	protected int line;
	protected char[] token;
	
	protected AbstractToken(char[] token,int line){
		this.token = token;
		this.line = line;
	}

	public IToken getNext() {
		return next;
	}

	public void scan(ITokenScanner scanner) {
		scanner.scan(this);
	}

	public void split(IToken... replacement) {
		AbstractToken prev = previous;
		for(IToken token : replacement){
			if(token instanceof AbstractToken){
				AbstractToken aToken = (AbstractToken)token;
				aToken.previous = prev;
				if(prev!=null)prev.next = aToken;
				prev = aToken;
			}
			else {
				throw new JSParseException("Token passed is not an AbstractToken");
			}
		}
		prev.next = next;
		if(next!=null){
			next.previous = prev;
		}
	}

	public int getLine() {
		return line;
	}

	public String toString() {
		return new String(token);
	}

	public void eliminate() {
		previous.next = next;
	}
}
